package ipad.asia.controller;

import ipad.asia.dao.OrderDao;
import ipad.asia.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

@RestController
public class OrderController {
    @Autowired
    private OrderDao orderDao;

    /***
     * @param shopId
     * @商家
     */
    //商家的所有订单
    @RequestMapping("/order/shop/shopId")
    public List<Order> findByShopId(@RequestParam(value = "shopId") int shopId){
        return orderDao.findByShopId(shopId);
    }


    //查看商家订单状态
    @RequestMapping("/order/shop/statues")
    public List<Order> findByIsCancelAndShopId(@RequestParam(value = "statues") int statues,
                                        @RequestParam(value = "shopId") int shopId){
        return orderDao.findByStatuesAndShopId(statues, shopId);
    }
    //商家按产品id查找商品订单
    @RequestMapping("/order/shop/productId")
    public List<Order> findByProductIdAndShopId(@RequestParam(value = "productId") int productId,
                                         @RequestParam(value = "shopId") int shopId){
        return orderDao.findByProductIdAndShopId(productId, shopId);
    }

    //商家按照日期查找订单
    @RequestMapping("/order/shop/dates")
    public List<Order> findByPayDateBetweenAndShopId(@RequestParam(value = "timeDown") LocalDateTime timeDown,
                                           @RequestParam(value = "timeUp") LocalDateTime timeUp,
                                           @RequestParam(value = "shopId") int shopId){
        return orderDao.findByPayDateBetweenAndShopId(timeDown, timeUp, shopId);
    }
    //商家删除订单
    @RequestMapping("/order/shop/delete")
    public void deleteByIdAndShopId(@RequestParam(value = "id") int id,
                        @RequestParam(value = "shopId") int shopId){
        orderDao.deleteByIdAndShopId(id, shopId);
    }
    //商家取消的订单
    @RequestMapping("/order/shop/get/cancel")
    public List<Order> findByShopCancel(@RequestParam(value = "id") int id,
                        @RequestParam(value = "shopId") int shopId){
        return orderDao.findByShopCancel(shopId);
    }
    /**
     * @param consumerId
     * @顾客
     */
    //顾客所有订单
    @RequestMapping("/order/consumer/consumerId")
    public List<Order> findByConsumerId(@RequestParam(value = "consumerId") int consumerId){
      return  orderDao.findByConsumerId(consumerId);
    }
    //顾客查找取消的订单
    @RequestMapping("/order/consumer/get/cancel")
    public List<Order> findByConsumerCancel(@RequestParam(value = "consumerId") int consumerId){
        return orderDao.findByConsumerCancel(consumerId);
    }
    //顾客查找商品的订单
    @RequestMapping("/order/consumer/productId")
   public List<Order> findByProductIdAndConsumerId(@RequestParam(value = "productId") int productId,
                                             @RequestParam(value = "consumerId") int consumerId){
        return orderDao.findByProductIdAndConsumerId(productId, consumerId);
    }
    //顾客查找订单状态
    @RequestMapping("/order/consumer/statues")
    public List<Order> findByStatuesAndConsumerId(@RequestParam(value = "statues") int statues,
                                             @RequestParam(value = "consumerId") int consumerId){
       return orderDao.findByStatuesAndConsumerId(statues, consumerId);
    }
    //顾客按照日期查找的订单
    @RequestMapping("/order/consumer/dates")
    public List<Order> findByPayDateBetweenAndConsumerId(@RequestParam(value = "timeDown") LocalDateTime timeDown,
                                               @RequestParam(value = "timeUp") LocalDateTime timeUp,
                                               @RequestParam(value = "consumerId") int consumerId){
        return orderDao.findByPayDateBetweenAndConsumerId(timeDown, timeUp, consumerId);
    }
//    //顾客取消订单
//    @RequestMapping("/order/cancelByConsumerId")
//    void cancelByConsumerId(@RequestParam(value = "id") int id,
//                            @RequestParam(value = "consumerId") int consumerId){
//         orderDao.cancelByConsumerId(id, consumerId);
//    }
    //顾客删除订单
    @RequestMapping("/order/consume/delete")
    public void deleteByIdAndConsumerId(@RequestParam(value = "id") int id,
                            @RequestParam(value = "consumerId") int consumerId){
         orderDao.deleteByIdAndConsumerId(id, consumerId);
    }

    /**
     * 更新订单状态
     * @param id
     * @param statues
     */
    @Transactional
    @RequestMapping("/order/setStatue")
    public void updateStatue(@RequestParam(value = "id") int id,
                             @RequestParam(value = "statues") int statues){
        orderDao.updateStatue(id, statues);
    }

    @Transactional
    @RequestMapping("/order/setPayStatue")
    public void updatePayStatue(@RequestParam(value = "id") int id,
                             @RequestParam(value = "statues") int statues,
                                @RequestParam(value = "payDate")Date payDate){
        orderDao.updatePayStatue(id, statues,payDate);
    }

    /**
     *
     * @param
     * @管理员
     */
    //查看全部订单
    @RequestMapping("/order/get/all")
    public List<Order> FindAll(){
        return orderDao.findAll();
    }
    //搜索产品的订单
    @RequestMapping("/order/id")
    public Order findId(@RequestParam(value = "id") int id){
        return orderDao.findById(id);
    }
    //查看订单状态
    @RequestMapping("/order/statues")
    public List<Order> findByStatues(@RequestParam(value = "statues") int statues){
        return orderDao.findByStatues(statues);
    }

    /**
     * 新增订单
     * @param consumerId
     * @param price
     * @param productId
     * @param number
     * @param describes
     * @param shopId
     * @param createDate
     */
    @RequestMapping( value = ("/order/add"))
    public void orderAdd(@RequestParam(value = "id",required = false) int id,
                         @RequestParam(value = "consumerId",required = false) int consumerId,
                         @RequestParam(value = "price",required = false) Float price,
                         @RequestParam(value = "productId",required = false) int productId,
                         @RequestParam(value = "number",required = false)int number,
                         @RequestParam(value = "describes",required = false)String describes,
                         @RequestParam(value = "shopId",required = false)int shopId,
                         @RequestParam(value = "address",required = false)String address,
                         @RequestParam(value = "statues",required = false)int statues,
                         @RequestParam(value = "createDate",required = false)Date createDate,
                         @RequestParam(value = "payDate",required = false) Date payDate) {
         Order order=new Order();
         order.setId(id).setConsumerId(consumerId).setPrice(price).setProductId(productId)
                 .setNumber(number).setDescribes(describes).setShopId(shopId).setAddress(address).setStatues(statues)
                 .setCreateDate(createDate).setPayDate(payDate);
         orderDao.save(order);
    }
    @Transactional
    @RequestMapping( value = ("/order/del"))
    public void orderDel(@RequestParam(value = "id",required = false) int id){
        orderDao.deleteById(id);
    }

    @RequestMapping("/order/findByShopIdAndStatues")
    public List<Order> findByShopIdAndStatues(@RequestParam(value = "shopId",required = false) int shopId,
                                              @RequestParam(value = "statues",required = false) int statues){
       return orderDao.findByShopIdAndStatues(shopId, statues);
    }


    @RequestMapping("findByShopIdAndIdAndStatues")
    public List<Order> findByShopIdAndId(@RequestParam(value = "shopId",required = false) int shopId,
                                             @RequestParam(value = "id",required = false) int id,
                                         @RequestParam(value = "statues",required = false)int statues){
       return orderDao.findByShopIdAndIdAndStatues(shopId,id,statues);
    }

}
